﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace XNADota.Terrain
{
    public class TexturePalette
    {
        public const byte CellSize = 64;

        private Texture _Texture;

        public Texture Texture
        {
            set { _Texture = value; }
            get { return _Texture; }
        }

        public static int GetType(int p00, int p10, int p01, int p11, int type)
        {
            // p00 --- p10
            //  |       |
            // p01 --- p11
            return GetType(p00 == type, p10 == type, p01 == type, p11 == type);
        }

        public static int GetType(bool b00, bool b10, bool b01, bool b11)
        {
            // b00 --- b10
            //  |       |
            // b01 --- b11
            int result = 0;
            result += b00 ? 1 : 0;
            result += b10 ? 2 : 0;
            result += b01 ? 4 : 0;
            result += b11 ? 8 : 0;
            return result;
        }

        public Rectangle GetRectangle(int type)
        {
            if (type == 0 || type > 15)
                throw new InvalidTerrainException("Texture type is out of range.");
            // 1 --- 2
            // |     |
            // 3 --- 4
            int x = 0;
            int y = 0;
            if ((type & 1) == 1) y += 2;
            if ((type & 2) == 2) y += 1;
            if ((type & 4) == 4) x += 2;
            if ((type & 8) == 8) x += 1;
            return  new Rectangle(x * CellSize, y * CellSize, CellSize, CellSize);
        }
    }
}
